package com.cus.tom.transfer.ac;

import android.os.Bundle;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.cus.tom.elec.utils.AdUtil;
import com.cus.tom.transfer.R;
import com.cus.tom.transfer.databinding.ActMainBinding;
import com.cus.tom.transfer.frag.ChangeFragment;
import com.cus.tom.transfer.frag.FileFragment;
import com.cus.tom.transfer.frag.MeFg;
import com.cus.tom.transfer.frag.RecordFragment;
import com.snap.base.base.BaseMainAct;
import com.snap.base.vm.DefaultVM;
import com.snap.util.BarUtilKt;

import me.jessyan.autosize.internal.CancelAdapt;

public class MainActivity extends BaseMainAct<ActMainBinding, DefaultVM> implements CancelAdapt {

    int current = -1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(null);
    }

    @NonNull
    @Override
    public DefaultVM createVM() {
        return new DefaultVM();
    }

    @Override
    public void doDataBind() {
    }

    @Override
    public int getContentViewId() {
        return R.layout.act_main;
    }

    @Override
    public void onAdInsertFromOtherActResume() {
        super.onAdInsertFromOtherActResume();
        AdUtil.Companion.adInsert(this);
    }

    @Override
    public void onCreate() {
        BarUtilKt.setStatusBarLight(this, true);
        binding.bottom.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.change) {
                    changeTab(0);
                } else if (checkedId == R.id.file) {
                    changeTab(1);
                } else if (checkedId == R.id.help) {
                    changeTab(2);
                } else if (checkedId == R.id.mine) {
                    changeTab(3);
                }
            }
        });
        binding.change.setChecked(true);
        AdUtil.Companion.adInsertDouble(this);
    }

    private void changeTab(int i) {
        AdUtil.Companion.adInsert(this);
        if (current == i) {
            return;
        }
        current = i;
        switch (current) {
            case 0:
                replaceFragment(new ChangeFragment());
                break;
            case 1:
                replaceFragment(new FileFragment());
                break;
            case 2:
                replaceFragment(new RecordFragment());
                break;
            case 3:
                replaceFragment(new MeFg());
                break;
        }
    }

    public void toMe() {
        binding.mine.setChecked(true);
    }

    private void replaceFragment(Fragment fragment) {
        try {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.container, fragment);
            transaction.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
